home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 April / MacFormat CD Edition MF36 (April 1996).iso / Shareware City / Developers / Tools Plus - GUI⁄Event libs / Tools Plus 2.6.1a Evaluat'n Kit / Tools Plus 2.6.1a / Tutorials / 4-Editing Fields / Tutorial.p < prev   
Encoding:
Text File  |  1995-08-25  |  6.6 KB  |  222 lines  |  [TEXT/PJMM]

  1. {    Tools Plus Tutorial    - -    Editing Fields    }
  2.  
  3. {    NOTE:    }
  4. {    The MWERKS compiler directived ($IFC MWERKS) indicates code that is compiled only by    }
  5. {    the CodeWarrior compiler.  THINK Pascal and Metrowerks CodeWarrior Pascal have    }
  6. {    _slight_ differences, and this compiler directive lets you use one source for both compilers.    }
  7. {    You can remove the code that does not pertain to your compiler.    }
  8.  
  9.  
  10. program Tutorial;
  11.     uses
  12. {$IFC MWERKS}
  13.         Dialogs, Fonts, Processes, SegLoad, TextEdit, ToolsPlus, Windows;
  14. {$ELSEC}
  15.         ToolsPlus;
  16. {$ENDC}
  17.  
  18.  
  19.     const
  20.     { Menu constants to make code more readable…}
  21.         ApplMenu = 0;
  22.         FileMenu = 1;
  23.         EditMenu = 2;
  24.  
  25.     { 'To Field' and 'From Field' buttons…}
  26.         bToField = 1;
  27.         bFromField = 2;
  28.  
  29.     type
  30.         Str30 = string[30];
  31.         Str30Ptr = ^Str30;
  32.         Str30Handle = ^Str30Ptr;    {Handle to 30-character editing field}
  33.  
  34.  
  35.     var
  36.         Poll: TPPollRecord;            {Polling record to retrieve event information}
  37.         ExitTheDemo: boolean;        {Should the demo terminate?}
  38.  
  39.         hField1: Str30Handle;            {Handle to 1st editing field's 30-char string    }
  40.         hField2, hField3: StringHandle;    {Handles to 2nd and 3rd editing fields' 255-char string    }
  41.         Field: integer;                    {Field number    }
  42.         EditString: Str255;                {Field's text    }
  43.         theButton: integer;                {Button number clicked in an alert }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  51.     procedure ProcessMenuSelection;
  52.         const
  53.             NewItem = 1;
  54.             OpenItem = 2;
  55.             CloseItem = 3;
  56.             QuitItem = 5;
  57.         var
  58.             theButton: integer;
  59.     begin
  60.  
  61.         case Poll.Menu.Num of
  62.             ApplMenu:     {Apple menu's 'About…' item}
  63.                 theButton := AlertBox(NoIcon, 'This is my application’s about box.  (Click to close)', NoButtonAlert);
  64.  
  65.             FileMenu: 
  66.                 case Poll.Menu.Item of
  67.                     NewItem: 
  68.                         ;
  69.                     OpenItem: 
  70.                         ;
  71.                     CloseItem: 
  72.                         ;
  73.                     QuitItem: 
  74.                         ExitTheDemo := true;
  75.                 end
  76.         end;
  77.  
  78.         MenuHilite(0);    {Turn off highlighted menu}
  79.     end;
  80.  
  81.  
  82.  
  83. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  84.     procedure ApplicationInitialization;
  85.         const
  86.             DemoWindow1 = 1;
  87.     begin
  88.     {    Do all the application setup before you start polling for events…    }
  89.  
  90.     {Create an Apple menu with an 'About…' item…}
  91.         AppleMenu('About My App…');
  92.  
  93.     {Create the standard File and Edit menus…}
  94.         Menu(FileMenu, 0, enabled, 'File');
  95.         Menu(FileMenu, 1, disabled, 'New/N');
  96.         Menu(FileMenu, 2, disabled, 'Open/O');
  97.         Menu(FileMenu, 3, disabled, 'Close/W');
  98.         Menu(FileMenu, 4, disabled, mDividingLine);
  99.         Menu(FileMenu, 5, enabled, 'Quit/Q');
  100.  
  101.         Menu(EditMenu, 0, enabled, 'Edit');
  102.         Menu(EditMenu, 1, disabled, 'Undo/Z');
  103.         Menu(EditMenu, 2, disabled, mDividingLine);
  104.         Menu(EditMenu, 3, disabled, 'Cut/X');
  105.         Menu(EditMenu, 4, disabled, 'Copy/C');
  106.         Menu(EditMenu, 5, disabled, 'Paste/V');
  107.         Menu(EditMenu, 6, disabled, 'Clear');
  108.         Menu(EditMenu, 7, disabled, mDividingLine);
  109.         Menu(EditMenu, 8, disabled, 'Show Clipboard…');
  110.         UpdateMenuBar;
  111.  
  112.  
  113.  
  114.         WindowOpen(DemoWindow1, 5, 40, 206, 312, 'Editing Field Tutorial', noGrowDocProc, GoAway, NotModal);
  115.  
  116.     { Allocate text handles for editing fields, and initialize to a null string…}
  117.         hField1 := Str30Handle(NewStrHandle(30));
  118.         hField2 := NewStrHandle(255);
  119.         hField3 := NewStrHandle(255);
  120.  
  121.     { Set each field's text to a default value…}
  122.         hField1^^ := 'Length limited field';
  123.         hField2^^ := 'Single line editing field.';
  124.         hField3^^ := 'This is a multiple-line editing field which incorporates word wrap.';
  125.  
  126.     { Create first field using Monaco 9pt.  It is length limited to 30 characters…}
  127.         TextFont(Monaco);
  128.         TextSize(9);
  129.         FieldLengthLimit(on);
  130.         NewField(1, 10, 10, 191, 21, Handle(hField1), teBoxNoCR, teJustLeft);
  131.         FieldLengthLimit(off);
  132.  
  133.     { Create second field using Geneva 9pt bold.  This is a single-line field…}
  134.         TextFont(Geneva);
  135.         TextFace([bold]);
  136.         NewField(2, 10, 30, 191, 42, Handle(hField2), teBoxNoCR, teJustLeft);
  137.  
  138.     { Create third field using Chicago 12pt.  This is a multiple line editing field…}
  139.         TextFont(systemFont);
  140.         TextSize(12);
  141.         TextFace([]);
  142.         NewField(3, 10, 51, 191, 227, Handle(hField3), teBoxCR, teJustLeft);
  143.  
  144.         ActivateField(1, teSelectEnd);    {Activate the first field with insertion point at the end of the text}
  145.  
  146.         NewButton(bToField, 6, 238, 95, 258, 'Write to #3', pushButProc, enabled, notSelected);
  147.         NewButton(bFromField, 105, 238, 195, 258, 'Get from #3', pushButProc, enabled, notSelected);
  148.         ExitTheDemo := false;
  149.     end;
  150.  
  151.  
  152. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  153. begin
  154. {$IFC MWERKS}
  155. {Toolbox initialization - - Done automatically by THINK Pascal}
  156.     InitGraf(@qd.thePort);
  157.     InitFonts;
  158.     InitWindows;
  159.     InitMenus;
  160.     TEInit;
  161.     InitDialogs(nil);
  162.     MaxApplZone;
  163. {$ENDC}
  164.  
  165.     if not InitToolsPlus(0, 1, UseColor) then
  166.         ExitToShell;
  167.  
  168.     ApplicationInitialization;
  169.  
  170.     while not ExitTheDemo do    {Main Event Loop}
  171.         if PollSystem(Poll) then    {If an event is available, process the event…}
  172.  
  173.             case Poll.What of
  174.  
  175.                 doMenu: 
  176.                     ProcessMenuSelection;
  177.  
  178.                 doGoAway:        {User clicked a window's close box}
  179.                     WindowClose(Poll.Window);
  180.  
  181.  
  182.                 doKeyDown, doAutoKey:    {Typing (check for Tab and Shift/Tab)…    }
  183.             { If key-stroke is in window 1, it's a tab, and the Command, Option and Control keys were not down…}
  184.                     if (Poll.Window = 1) and (Poll.Key.Chr = TabKey) and not (Poll.Modifiers.CmdKey or Poll.Modifiers.OptionKey or Poll.Modifiers.ControlKey) then
  185.                         begin
  186.                             SaveFieldString;                        {Save the field's edited text as the field's string    }
  187.                             Field := ActiveFieldNumber;            {Determine the active field number    }
  188.                             if not Poll.Modifiers.ShiftKey then    {TAB: to next field…    }
  189.                                 Field := Field + 1 - 3 * ord(Field = 3)    {    Add 1.  If field=3, start at 1 again.    }
  190.                             else                                    {SHIFT-TAB: to previous field…    }
  191.                                 Field := Field - 1 + 3 * ord(Field = 1);    {    Subtract 1.  If field=1, start at 3.    }
  192.                             ActivateField(Field, teSelectAll);        {Select all the text in the newly activated field    }
  193.                         end;
  194.  
  195.  
  196.                 doClickField:    {User clicked in an inactive field…}
  197.                     begin
  198.                         CurrentWindow(1);    {The following commands apply to window #1…}
  199.                         SaveFieldString;    {Save the field's edited text as the field's string    }
  200.                         ClickInField;        {Process the click in the inactive field}
  201.                     end;
  202.  
  203.  
  204.                 doButton:    {User clicked a button… }
  205.                     case Poll.Button.Num of
  206.                         bToField:{Write into field #3…}
  207.                             PasteIntoField(3, 'This is the replacement text pasted by the application', teReplace);
  208.  
  209.                         bFromField:    {Display contents of field #3…}
  210.                             begin
  211.                                 if ActiveFieldNumber = 3 then    {If user is editing field #3…}
  212.                                     GetFieldString(EditString)    {    get the EDITED text.}
  213.                                 else
  214.                                     EditString := hField3^^;        {Copy field's (permanent) text to EditString}
  215.                                 theButton := AlertBox(noteIcon, EditString, -OkAlert);
  216.                             end
  217.                     end;
  218.  
  219.  
  220.                 otherwise    {All other events are ignored}
  221.             end
  222. end.